﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace ST.Common.Util
{
	public struct LVITEM
	{
		public int mask;
		public int iItem;
		public int iSubItem;
		public int state;
		public int stateMask;
		public IntPtr pszText; // string 
		public int cchTextMax;
		public int iImage;
		public IntPtr lParam;
		public int iIndent;
		public int iGroupId;
		public int cColumns;
		public IntPtr puColumns;
	}

	[StructLayout(LayoutKind.Sequential)]
	public struct RECT
	{
		public int left;
		public int top;
		public int right;
		public int bottom;
	}

	public static class Win32Helper
	{
		#region WIN32相关
		public const uint LVM_FIRST = 0x1000;
		public const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
		public const uint LVM_GETITEMW = LVM_FIRST + 75;

		public static int LVIF_TEXT = 0x0001;

		public const uint PROCESS_VM_OPERATION = 0x0008;
		public const uint PROCESS_VM_READ = 0x0010;
		public const uint PROCESS_VM_WRITE = 0x0020;

		public const int BM_CLICK = 0xF5;

		public const uint MEM_COMMIT = 0x1000;
		public const uint MEM_RELEASE = 0x8000;
		public const uint MEM_RESERVE = 0x2000;

		public const uint PAGE_READWRITE = 4;

		public const int WM_CLOSE = 0x0010;
		public const int WM_GETTEXT = 0x000D;
		public const int WM_SETTEXT = 0x000C;

		/// <summary>
		/// 获取选中的索引
		/// </summary>
		public const uint CB_GETCURSEL = 0x0147;
		/// <summary>
		/// 获取选择的文本
		/// </summary>
		public const uint CB_GETLBTEXT = 0x0148;
		public const uint CB_SETCURSEL = 0x014E;
		public const uint CB_ADDSTRING = 0x0143;
		public const uint CB_INSERTSTRING = 0x014A;
		public const uint CB_SELECTSTRING = 0x014D;

		/// <summary>
		/// 发送消息，SendMessage是等到消息被处理后才返回
		/// </summary>
		/// <param name="hwnd"></param>
		/// <param name="msg"></param>
		/// <param name="wParam"></param>
		/// <param name="lParam"></param>
		/// <returns></returns>
		[DllImport("user32.dll", SetLastError = false)]
		public static extern int SendMessage(IntPtr hwnd, uint msg, int wParam, int lParam);

		[DllImport("user32.dll", SetLastError = false)]
		public static extern int SendMessage(IntPtr hwnd, uint msg, int wParam, string lParam);

		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
		public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

		[DllImport("user32.dll", SetLastError = false)]
		public static extern int PostMessage(IntPtr hwnd, uint msg, int wParam, int lParam);

		[DllImport("user32.DLL", SetLastError = false)]
		public static extern IntPtr FindWindow(string lpClsName, string lpWndName);

		/// <summary>
		/// 查找第一个子窗口
		/// </summary>
		/// <param name="hwndParent">父窗口</param>
		/// <param name="hwndChildAfter">兄弟窗口句柄</param>
		/// <param name="strClass"></param>
		/// <param name="strWindow"></param>
		/// <returns></returns>
		[DllImport("user32.DLL", SetLastError = true)]
		public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClsName, string lpWndName);

		/// <summary>
		/// 根据句柄获得进程id值
		/// </summary>
		/// <param name="handle">句柄</param>
		/// <param name="pid"></param>
		/// <returns></returns>
		[DllImport("user32.dll")]
		public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);

		/// <summary>
		/// 返回true继续枚举，否则终止
		/// </summary>
		/// <param name="hwnd"></param>
		/// <param name="lParam"></param>
		/// <returns></returns>
		public delegate bool EnumChildWindowsCallback(IntPtr hwnd, int lParam);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="hWndParent"></param>
		/// <param name="lpfn"></param>
		/// <param name="lParam">通常传0</param>
		/// <returns></returns>
		[DllImport("user32.dll")]
		public static extern IntPtr EnumChildWindows(IntPtr hWndParent, EnumChildWindowsCallback lpfn, IntPtr lParam);

		public delegate bool EnumWindowsCallback(IntPtr hwnd, IntPtr lParam);
		[DllImport("user32.dll")]
		public static extern bool EnumWindows(EnumWindowsCallback lpfn, IntPtr lParam);

		///// <summary>
		///// 通常使用此函数获取标题（支持中文）
		///// </summary>
		///// <param name="hwnd"></param>
		///// <param name="strOut"></param>
		///// <param name="maxCount"></param>
		///// <returns></returns>
		//[DllImport("user32.dll", EntryPoint = "GetWindowTextA")]
		//public static extern int GetWindowTextA(IntPtr hwnd, System.Text.StringBuilder strOut, int maxCount);

		//[DllImport("user32.dll", EntryPoint = "GetWindowTextW")]
		//public static extern int GetWindowTextW(IntPtr hwnd, System.Text.StringBuilder strOut, int maxCount);

		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
		public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpText, int nMaxCount);

		//[DllImport("user32.dll", EntryPoint = "GetWindowTextLengthA")]
		//public static extern int GetWindowTextLengthA(IntPtr hwnd);

		//[DllImport("user32.dll", EntryPoint = "GetWindowTextLengthW")]
		//public static extern int GetWindowTextLengthW(IntPtr hwnd);

		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
		public static extern int GetWindowTextLength(IntPtr hwnd);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern bool IsWindowVisible(IntPtr hWnd);

		/// <summary>
		/// 获取窗体的类名
		/// </summary>
		/// <param name="hwnd"></param>
		/// <param name="buf"></param>
		/// <param name="nMaxCount"></param>
		/// <returns></returns>
		[DllImport("user32.dll")]
		public static extern int GetClassName(IntPtr hwnd, StringBuilder buf, int nMaxCount);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="hwnd"></param>
		/// <param name="rect"></param>
		/// <returns></returns>
		[DllImport("user32.dll")]
		public static extern bool GetWindowRect(IntPtr hwnd, out RECT rect);

		public enum SW_Cmd { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_SHOWNOACTIVATE = 4, SW_RESTORE = 9, SW_SHOWDEFAULT = 10 }
		[DllImport("user32.dll")]
		private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
		public static bool ShowWindowAsync(IntPtr hWnd, SW_Cmd nCmdShow)
		{
			return ShowWindowAsync(hWnd, (int)nCmdShow);
		}

		[DllImport("user32.dll")]
		public static extern bool IsIconic(IntPtr hWnd);

		[DllImport("user32.dll")]
		public static extern bool IsZoomed(IntPtr hWnd);

		[DllImport("user32.dll")]
		public static extern IntPtr GetForegroundWindow();

		[DllImport("user32.dll")]
		public static extern bool SetForegroundWindow(IntPtr hWnd);

		[DllImport("user32.dll")]
		public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

		[DllImport("user32.dll")]
		public static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);

		[DllImport("user32.dll")]
		public static extern bool SetCursorPos(int X, int Y);

		[DllImport("kernel32.dll")]
		public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);


		[DllImport("kernel32.dll")]
		public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

		[DllImport("kernel32.dll")]
		public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);

		[DllImport("kernel32.dll", SetLastError = true)]
		//[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool CloseHandle(IntPtr handle);

		[DllImport("kernel32.dll")]
		public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

		[DllImport("kernel32.dll")]
		public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);


		[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
		public static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);

		[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern Boolean DeviceIoControl(IntPtr hDevice, UInt32 dwIoControlCode, ref UInt32 lpInBuffer, Int32 nInBufferSize, Byte[] lpOutBuffer, Int32 nOutBufferSize, out Int32 nBytesReturned, IntPtr lpOverlapped);

		#endregion
	}
}
